package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.DepartmentMapper;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.PageResult;
import cn.wolfcode.rbac.util.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2020/7/10 0010.
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Department department) {
        if(department.getId() == null){
            departmentMapper.insert(department);
        }else{
            departmentMapper.updateByPrimaryKey(department);
        }
    }




    /**
     * 分页的 业务
     * @param queryObject   分页的参数
     * @return
     */
    @Override
    public PageResult query(QueryObject queryObject) {
        // 1 如果数据库中表没有数据那么需要分页吗 , 肯不不需要分页
        // count 就是数据库有多少条数据
       int count =  departmentMapper.selectForCount(queryObject);
       if(count == 0){
           return PageResult.DEFAULT_DATA;
       }
        // 2 如果表中有数据 那么需要分页..
        List<Department>  list = departmentMapper.selectForList(queryObject);
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }

}
